<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper" style='margin-bottom: 10px'>
<!--    查询区域-->
      <a-form layout='inline'>
        <a-row :gutter='12'>
          <a-col :md='4' :sm='8'>
            <a-form-item label='所属项目'>
              <a-select show-search :filter-option="filterOption"  v-model='queryParam.projectId' @change='initAreaList' placeholder='请输入所属项目' style='margin-right: 20px'>
                <a-select-option value=''>请选择项目</a-select-option>
                <a-select-option v-for='item in projectList' :label='item.name' :value='item.id' :key='item.id'>
                  <span style="float: left">{{item.name}}</span>
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md='5' :sm='8'>
            <a-form-item label='上级区域名称'>
              <a-select show-search :filter-option="filterOption"  v-model='queryParam.topAreaId' placeholder='请输入上级区域名称'>
                <a-select-option value=''>请输入上级区域</a-select-option>
                <a-select-option v-for='item in topAreaList' :label='item.name' :value='item.id' :key='item.id'>
                  <span style="float: left">{{item.name}}</span>
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md='5' :sm='8'>
            <a-form-item label='区域名称'>
              <a-input v-model='queryParam.areaName' placeholder='请输入区域名称'></a-input>
            </a-form-item>
          </a-col>
          <a-col :md='4' :sm='8'>
            <a-form-item label='创建人'>
              <a-input v-model='queryParam.createUser' placeholder='请输入创建人名称'></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="10" :sm="12">
            <a-form-item label="创建时间" :labelCol="{span: 5}" :wrapperCol="{span: 18}">
              <j-date v-model="queryParam.createTime_begin" :showTime="true" date-format="YYYY-MM-DD HH:mm:ss" style="width:45%" placeholder="请选择开始时间" ></j-date>
              <span style="width: 10px;">~</span>
              <j-date v-model="queryParam.createTime_end" :showTime="true" date-format="YYYY-MM-DD HH:mm:ss" style="width:45%" placeholder="请选择结束时间"></j-date>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button
                type="primary"
                @click="searchReset"
                icon="reload"
                style="margin-left: 8px"
              >重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <a-table
      ref="table"
      bordered
      size="middle"
      rowKey="id"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
      :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
      @change="handleTableChange"
    >
    </a-table>
  </a-card>

</template>
<script>
import AreaTree from './AreaTree'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { httpAction, postAction, getAction, deleteAction } from '@api/manage'
import { filterObj } from '@/utils/util'
import { getProjectList } from '@api/api'
export default{
  name:'AreaList',
  //分页
  mixins:[JeecgListMixin],
  //引入组件
  components:{
    AreaTree,
  },
  data(){
    return{
      columns:[
        {
          title:'序号',
          dataIndex:"",
          key:'rowIndex',
          width:60,
          align:'center',
          customRender:function(t,r,index){
            return parseInt(index)+1
          }
        },
        {
          title:'所属项目',
          align: "center",
          dataIndex: 'projectName',
          width: 100,

        },
        {
          title:'区域名称',
          align:"center",
          dataIndex:'areaName',
          customRender:function(text){
            if (text==null||text==''){
              return '无'
            }else {
              return text
            }
          },
          width: 100
        },
        {
          title:'上级区域',
          align:"center",
          dataIndex:'parentName',
          width: 50
        },
        {
          title:'子区域数',
          align:"center",
          dataIndex: 'childrenAreaNum',
          width:50
        },
        {
          title:'房屋数',
          align:"center",
          dataIndex: 'houseNum',
          width:50
        },
        {
          title:'人口数',
          align:"center",
          dataIndex: 'peopleNum',
          width:50
        },
        {
          title:'备注',
          align:"center",
          dataIndex: 'remark',
          width:100
        },
        {
          title:'创建时间',
          align:"center",
          dataIndex: 'createDateTime',
          width:100
        },
        {
          title:'创建人',
          align:'center',
          dataIndex: 'createUser',
          width: 100
        },


      ],
      topAreaList:[],
      projectList:[],
      url:{
        list:'/area/areaVoList',
        getAreaInfos:'/area/getAreaList',
        deleteArea: '/area/delete',
      },
      queryParam:{
        "areaName":"",
        "createUser":"",
        "createTime_begin":"",
        "createTime_end":"",
        "projectNum":""
      },
    }
  },
  computed:{
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  created() {
    this.keyupEnter()
    this.initProjectList()
  },
  methods:{
    filterOption(input, option) {
      let com = option.componentOptions
      let name = String(com.propsData.label)
      if(name.toLowerCase().includes(input.toLowerCase())){
        return true
      }
    },
    initAreaList(e) {
      let params = {
        projectId:e
      }
      this.queryParam.topAreaId=''
      getAction(this.url.getAreaInfos,params).then((res)=>{
        if(res.success){
          if (res.result!=null){
            this.topAreaList = res.result
          }
        }
      })
    },
    bind(record){
      this.$refs.selectProjectModal.setAreaId(record.areaId)
      this.$refs.selectProjectModal.title = '绑定项目'
    },
    initProjectList() {
      getProjectList().then((res)=>{
        if(res.success){
          this.projectList = res.result
        }
      })
    },
    handleEdit:function(record){

    },
    handleAdd:function(){

    },
    //加载
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      if (arg === 1) {
        this.ipagination.current = 1
      }
      let params = this.getQueryParams()
      this.loading = true
      console.log(params)
      getAction(this.url.list, params).then(res => {
        if (res.success) {
          if (res.result!=null){
            this.dataSource = res.result.records
            this.ipagination.total = res.result.total
          }


        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },
    searchQuery() {
      let startTime = this.queryParam.createTime_begin;
      let endTime = this.queryParam.createTime_end;

      if (startTime != "" && endTime != "") {
        if (startTime > endTime) {
          this.$message.error("开始时间不能大于结束时间");
          return
        }
      }

      if (startTime == "" && endTime != "") {
        this.$message.error("请选择开始时间");
        return
      }

      if (startTime != "" && endTime == "") {
        this.$message.error("请选择结束时间");
        return
      }

      this.loadData(1)
    },
    getQueryParams() {
      let param = Object.assign({}, this.isorter, this.queryParam)
      param.field = this.getQueryField()
      param.pageNo = this.ipagination.current
      param.pageSize = this.ipagination.pageSize
      param.areaName = this.queryParam.areaName
      param.createUser =  this.queryParam.createUser
      param.projectNum = this.queryParam.projectNum
      if (this.queryParam.createTime_begin != "" && this.queryParam.createTime_end != "") {
        param.startTime = this.queryParam.createTime_begin + " 00:00:00";
        param.endTime = this.queryParam.createTime_end + " 23:59:59";
      }
      return filterObj(param)
    },
    deleteArea: function(record) {
      console.log(record)
      deleteAction(this.url.deleteArea,{id:record.id}).then(res => {
        if (res.success) {
          this.$message.success(res.message)
          this.loadData()
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    keyupEnter() {

      document.onkeydown = e => {
        let body = document.getElementsByTagName('body')[0]
        if (e.keyCode === 13) {
          this.loadData(1)
        }
      }
    },

  }
}
</script>